package com.xteam.xnetworkdisk.sys.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/sys/session")
public class SessionController {

    /**
     *
     * @param session
     * @return
     */

    @GetMapping("/set")
    public String setSession(HttpSession session, HttpServletResponse response) {
        session.setAttribute("userid", 1);
        // 将用户ID保存到session
        session.setMaxInactiveInterval(60 * 60); // 设置Session的有效期为1小时
        return "Session data set";
    }

    @GetMapping("/set1")
    public String setSession1(HttpSession session) {
        session.setAttribute("userid", 1);
        return "Session data set";
    }
    /**
     *
     * @param session
     * @return
     */
    @GetMapping("/get")
    public String getSession(HttpSession session) {
        Integer userid = (Integer) session.getAttribute("userid");
        return "Session data: " + userid;
    }

    /**
     *
     * @param session
     * @return
     */

    @GetMapping("/invalidate")
    public String invalidateSession(HttpSession session) {
        session.invalidate();
        return "Session invalidated";
    }
}
